package exercise.jslide.jslide.view.dialog;

import exercise.jslide.jslide.utils.SerializableFont;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.stage.*;

public class FontDialog {
    private Stage stage;
    private ComboBox<String> fontFamilyComboBox;
    private ComboBox<Integer> fontSizeComboBox;
    private SerializableFont selectedFont;

    // 构造函数
    public FontDialog(Stage parentStage) {
        stage = new Stage();
        stage.initOwner(parentStage); // 设置父窗口
        stage.initModality(Modality.WINDOW_MODAL); // 设置为模态对话框
        stage.setTitle("选择字体");
        stage.getIcons().add(new Image(getClass().getResourceAsStream("/exercise/jslide/jslide/app/icons/LOGO.png")));


        fontFamilyComboBox = new ComboBox<>();
        // 添加系统字体
        fontFamilyComboBox.getItems().addAll(Font.getFamilies());

        fontSizeComboBox = new ComboBox<>();
        fontSizeComboBox.getItems().addAll(10, 12, 14, 16, 18, 20, 24, 28, 32); // 添加字体大小选项

        //初始化字体和字号
        fontFamilyComboBox.setValue("微软雅黑");
        fontSizeComboBox.setValue(14);

        Button okButton = new Button("确定");
        okButton.setOnAction(e -> {
            selectedFont = new SerializableFont(fontFamilyComboBox.getValue(), fontSizeComboBox.getValue());
            stage.close();
        });

        VBox layout = new VBox(20);
        layout.setPadding(new Insets(20)); // 设置内边距为20
        layout.setAlignment(Pos.CENTER); // 设置对齐方式为居中
        layout.getChildren().addAll(new Label("字体:"), fontFamilyComboBox, new Label("大小:"), fontSizeComboBox, okButton);
        Scene scene = new Scene(layout, 300, 200);
        stage.setScene(scene);
    }

    // 显示对话框
    public void show() {
        stage.showAndWait();
    }

    // 隐藏对话框
    public void hide() {
        stage.close();
    }

    // 获取选择的字体
    public SerializableFont getSelectedFont() {
        return selectedFont;
    }
}